package com.ruix.app.ui.stratum.lesson;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.ui.base.BaseActivity;
import com.ruix.app.widget.CommonTitleBar;
import com.ruix.app.widget.SumupItemView;
import com.ruix.app.widget.dialog.CommonDialog;

import java.util.ArrayList;

/**
 * Created by hadexs on 2017/5/18.
 */

public class LessonSumupActivity extends BaseActivity implements SumupItemView.DeleteTargeListener{



    private ArrayList<SumupItemView> targetViews;

    public static Intent getInstent(Context contxt){
        Intent intent = new Intent(contxt,LessonSumupActivity.class);
        return intent;
    }

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_lesson_sumup;
    }

    private LinearLayout listView;

    @Override
    protected void initView() {
        setTitle("本期干货");
        listView = (LinearLayout) findViewById(R.id.ll_contain_target);
        targetViews = new ArrayList<>();
        TextView textView = (TextView)findViewById(R.id.tv_add_target);
        textView.setText(Html.fromHtml("<u>"+"添加一个段落"+"</u>"));

        CommonTitleBar titleBar = getTitleBar();
        TextView titleBarRightBtn = titleBar.getRightBtn();
        titleBarRightBtn.setText("发送");
        titleBarRightBtn.setTextColor(getResources().getColor(R.color.green));
        titleBarRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendTarget();
            }
        });
        ImageView leftBackBtn = titleBar.getLeftBackBtn();
        leftBackBtn.setImageResource(R.drawable.close);

        addTargtView();
    }



    private String buildTargetString(){
        StringBuffer sb = new StringBuffer();

        if (targetViews != null ){
            for (int i = 0; i < targetViews.size(); i++) {
                SumupItemView targeview=  targetViews.get(i);
                String inputValue =  targeview.getInputValue();
                if (!TextUtils.isEmpty(inputValue)){
                    sb.append(inputValue + "\n\n");
                }
            }
        }
        return sb.toString();
    }

    private void sendTarget(){
        String reust =   buildTargetString();
        if (TextUtils.isEmpty(reust)){
            showErrorTip("请输入本期干货");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("sumup",reust);
        setResult(RESULT_OK,intent);
        finish();

    }

    private void addTargtView(){
        SumupItemView view = new SumupItemView(this);
        view.setListener(this);
        listView.addView(view);
        targetViews.add(view);
    }


    @Override
    protected void initLocalData() {

    }

    @Override
    protected void initListener() {
        findViewById(R.id.tv_add_target).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (targetViews.size() == 0){
                    addTargtView();
                } else {
                    SumupItemView targeview = targetViews.get(targetViews.size() - 1);
                    String inputValue = targeview.getInputValue();
                    if (TextUtils.isEmpty(inputValue)){
                        showErrorTip("请完善当前段落干货");
                        return;
                    }
                    addTargtView();
                }
            }
        });
    }

    @Override
    public void removeView(View view) {
        SumupItemView sumupItemView =  (SumupItemView)view;
        String input =  sumupItemView.getInputValue();
        if (TextUtils.isEmpty(input)){
            removeContentView(view);
        } else {
            showConfirmDialog(view);
        }


    }

    private void removeContentView(View view){
        if (targetViews.size() == 1){
            SumupItemView sumupItemView =  targetViews.get(0);
            sumupItemView.clearInput();
        } else {
            listView.removeView(view);
            targetViews.remove(view);
        }
    }

    private void showConfirmDialog(final View view){
        CommonDialog dialog = new CommonDialog(this);
        dialog.setListener(new CommonDialog.CommonDialogAcitonListener() {
            @Override
            public void positiveAction() {
                removeContentView(view);
            }

            @Override
            public void negertivieAction() {

            }
        });
        dialog.show("是否要删除此条干货？");

    }




    @Override
    public void afterTextChanged(boolean hasValue) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (targetViews!= null) {
            targetViews.clear();
            targetViews = null;
        }
    }
}
